﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Windows.Forms;
using Bronze.XXT.PhoneServer;

namespace 模拟话机
{
    /// <summary>
    /// YGJ
    /// 2013-2-28
    /// 当前测试窗体仅适用话机向服务器发起的请求
    /// 尚未处理返回的数据
    /// 添加一个测试时，将新按钮的Tag赋值MsgAttribute fun_no
    /// </summary>
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            propertyValues = new Dictionary<string, string>();

            serverIpEndPoint = new IPEndPoint(new IPAddress(new byte[] { 127, 0, 0, 1 }), 2555);
        }


        /// <summary>
        /// 服务端IP地址
        /// </summary>
        private IPEndPoint serverIpEndPoint;
        /// <summary>
        /// 用到的Body属性值
        /// </summary>
        private Dictionary<string, string> propertyValues;

        private void btn_click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            if (string.IsNullOrEmpty(btn.Tag.ToString()) || btn.Tag == null)
            {
                MessageBox.Show("这个按钮的Tag属性没有绑定相应的MessageBodID！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }



        }
        /// <summary>
        /// 根据MsgAttribute ID属性获取Body的类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private Type GetBodyTypeByID(string id)
        {
            MsgAttribute attr = null;
            string strKey = string.Empty;

            Assembly asm = Assembly.Load("Bronze.XXT.PhoneServer");
            var types = asm.GetTypes();
            var baseType = typeof(MessageBody);

            ProgramCache.MESSAGEBODY_TYPES_CACHE.Clear();

            foreach (Type type in types)
            {
                if (baseType.IsAssignableFrom(type))
                {
                    var attrs = type.GetCustomAttributes(typeof(MsgAttribute), false);
                    if (attrs != null && attrs.Any())
                    {
                        attr = attrs[0] as MsgAttribute;
                    }
                    if (attr != null && attr.func_no.Equals(id))
                    {
                        return type;
                    }
                }
            }
            return null;
        }
        /// <summary>
        /// Body可能用到的属性值
        /// </summary>
        private void BuildProperties()
        {
            propertyValues.Add("Device_msisdn", "");
            propertyValues.Add("Device_id", "");
            propertyValues.Add("card_id", "");
            propertyValues.Add("student_id", "");
            propertyValues.Add("request_time", "");
            propertyValues.Add("Call_times", "");
            propertyValues.Add("Sms_Asicc_Cont", "");
            propertyValues.Add("start_time", "");
            propertyValues.Add("Reserved", "");
        }
        /// <summary>
        /// 设置Body的属性值
        /// </summary>
        /// <param name="body"></param>
        /// <param name="bodyType"></param>
        private void SetBodyPropertyValue(MessageBody body, Type bodyType)
        {
            var properties = bodyType.GetProperties(BindingFlags.Public);
            foreach (PropertyInfo per in properties)
            {
                if (propertyValues.ContainsKey(per.Name))
                {
                    per.SetValue(body, propertyValues[per.Name], null);
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            BuildProperties();
        }

        private Byte[] GetBytes(string id)
        {
            Bronze.XXT.PhoneServer.Message msg = new Bronze.XXT.PhoneServer.Message();
            Type bodyType = GetBodyTypeByID(id);
            if (bodyType == null)
            {
                MessageBox.Show("未找到相应的类型！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return null;
            }
            msg.Body = Activator.CreateInstance(bodyType) as MessageBody;

            SetBodyPropertyValue(msg.Body, bodyType);

            return msg.Body.GetBytes();
        }

        private void SendMsg(string id)
        {
            TcpClient client = new TcpClient();
            client.Connect(serverIpEndPoint);
            NetworkStream netStream = client.GetStream();
            try
            {
                var bytes = GetBytes(id);
                if (bytes == null) 
                {
                    MessageBox.Show("生成数据失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                netStream.Write(bytes, 0, bytes.Length);
                //netStream.Read(bytes, 0, 1024);
            }
            catch
            {

            }
        }
    }
}
